# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :file_type.py
# @Time      :2023/10/13 14:26
# @Author    :WangXiang
import sys
import random
import string
import os


def generate_random_text(size):
    # 生成指定大小的随机英文字符
    return ''.join(random.choices(string.ascii_letters, k=size))


def create_folder_with_files(folder_path="./my_foder", num_files=10, total_size="10MB"):
    """创建指定大小的文件夹，可以指定文件夹大小，文件个数，生成路径"""
    # 创建文件夹
    os.makedirs(folder_path, exist_ok=True)
    # 转成字节
    if "MB" in total_size:
        total_size = int(total_size.split("MB")[0]) * 1024 * 1024
    elif "GB" in total_size:
        total_size = float(total_size.split("GB")[0]) * 1024 * 1024 * 1024
    # 计算每个文件的平均大小
    avg_file_size_bytes = total_size // num_files

    for i in range(num_files):
        # 随机生成每个文件的大小
        file_size_bytes = avg_file_size_bytes
        file_name = f"File{i + 1}.txt"

        # 生成随机文本内容
        file_content = generate_random_text(file_size_bytes)

        # 写入文件
        with open(os.path.join(folder_path, file_name), 'w') as file:
            file.write(file_content)
        print(f"Created file: {file_name} ({file_size_bytes} bytes)")


def create_file_size(num_size="1GB", path="./"):
    """创建指定大小的文件"""
    file_path = path + num_size + ".txt"
    print("This is method 2.", num_size)

    # print(num_size[:1])
    if "MB" in num_size:
        size_bytes = int(num_size.split("MB")[0]) * 1024 * 1024
    elif "GB" in num_size:
        size_bytes = int(num_size.split("GB")[0]) * 1024 * 1024 * 1024
    # # 使用二进制写入方式创建文件并写入数据

    block_size = 1024
    # num_blocks = size_bytes // block_size
    with open(file_path, 'w') as file:
        # 计算需要生成的块数
        num_blocks = size_bytes // block_size

        # 生成每个块的随机英文文本并写入文件
        for _ in range(num_blocks):
            # 生成随机英文文本
            random_text = ''.join(random.choices(string.ascii_letters, k=block_size))

            # 写入文件
            file.write(random_text)

        # 处理剩余的不足一块的部分
        remaining_bytes = size_bytes % block_size
        if remaining_bytes > 0:
            random_text = ''.join(random.choices(string.ascii_letters, k=remaining_bytes))
            file.write(random_text)
    print(f"File created: {file_path}")


if __name__ == "__main__":
    # 获取命令行参数
    method_name = sys.argv[1]
    # 创建指定大小的文件
    if method_name == "create_file_size":
        if len(sys.argv) == 3:
            print(sys.argv)
            create_file_size(num_size=sys.argv[2])
        elif len(sys.argv) == 4:
            create_file_size(num_size=sys.argv[2], path=sys.argv[3])
        elif len(sys.argv) == 2:
            create_file_size()

    elif method_name == "create_folder_with_files":
        """创建指定大小的文件夹，可以指定文件夹大小，文件个数，生成路径"""
        if len(sys.argv) == 3:
            # 指定文件路径
            create_folder_with_files(folder_path=sys.argv[2])
        elif len(sys.argv) == 4:
            """指定文件夹路径，文件个数"""
            create_folder_with_files(folder_path=sys.argv[2], num_files=int(sys.argv[3]))
        elif len(sys.argv) == 5:
            """指定文件夹路径，文件个数, 文件夹大小"""
            create_folder_with_files(folder_path=sys.argv[2], num_files=int(sys.argv[3]), total_size=sys.argv[4])
        elif len(sys.argv) == 2:
            # 使用默认值
            create_folder_with_files()

    else:
        print("Invalid method name.")
